#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <math.h>
#include "tw2865.h"

//#define NORMAL

#ifdef NORMAL
int main()
{
	int dev;
	struct tw2865_reg twreg;

	dev = open("/dev/tw2865dev", O_RDONLY);

	/* read all register */
	twreg.addr = 0x50;
	//ioctl(dev, TW2865_GET_ALL_REG, &twreg);


	/* read */
	twreg.addr = 0x50;
	twreg.reg = 0x43;
	twreg.value = 0;
	ioctl(dev, TW2865_GET_SIGNLE_REG, &twreg);
	printf("addr = 0x%x, reg = 0x%x, value = 0x%x\n", twreg.addr, twreg.reg, twreg.value);


	/* write */
	twreg.addr = 0x50;
	twreg.reg = 0x43;
	twreg.value = 0xdd;
	ioctl(dev, TW2865_SET_SIGNLE_REG, &twreg);
	//printf("addr = 0x%x, reg = 0x%x, value = 0x%x\n", twreg.addr, twreg.reg, twreg.value);


	/* read */
	twreg.addr = 0x50;
	twreg.reg = 0x43;
	twreg.value = 0;
	ioctl(dev, TW2865_GET_SIGNLE_REG, &twreg);
	printf("addr = 0x%x, reg = 0x%x, value = 0x%x\n", twreg.addr, twreg.reg, twreg.value);


	close(dev);

	return 0;
}

#else

void printhelp(void)
{
	printf("1--pring all register values.\n" );
	printf("2--write a register\n" );
	printf("3--read a register\n" );	
}

int main(int argc,char* argv[])
{
	int dev;
	unsigned int addr;
	unsigned int reg;
	unsigned int value;
	struct tw2865_reg twreg;
	int para ;
	
	if (argc != 2) {
    	printf("usage :%s 1|2|3\n",argv[0]);
		printhelp();
		return 0;
	}
	
	dev = open("/dev/tw2865dev", O_RDONLY);
	if (dev <= 0) {
		printf("tw2865dev device open fail\n");
		exit(0);
	}

	para = atoi(argv[1]);
	switch(para)
	{
		case 1:	
			/* 
			 * read all register value . for example 
			 */
			printf("please input I2C addr:\n");
			scanf("%x",&addr);			
			
			twreg.addr = (unsigned char)addr;
			twreg.reg = 0;
			twreg.value = 0;
			ioctl(dev, TW2865_GET_ALL_REG, &twreg);
			break ;

		case 2:	/* write */
			printf("(write...)please input I2C addr, reg and reg-value:\n") ;
			scanf("%x %x %x",&addr,&reg,&value);			
			//printf("addr = 0x%x, reg = 0x%x, value = 0x%x\n", addr, reg, value);

			twreg.addr = (unsigned char)addr;
			twreg.reg = (unsigned char)reg;
			twreg.value = (unsigned char)value;			
			ioctl(dev, TW2865_SET_SIGNLE_REG, &twreg);
			printf("addr = 0x%x, reg = 0x%x, value = 0x%x\n", twreg.addr, twreg.reg, twreg.value);
			break ;
		
		case 3:	/* read */
			printf("(read...)please input I2C addr, reg:\n") ;
			scanf("%x %x",&addr,&reg);	
			//printf("addr = 0x%x, reg = 0x%x\n", addr, reg);
			
			twreg.addr = (unsigned char)addr;
			twreg.reg = (unsigned char)reg;
			twreg.value = 0;	
			ioctl(dev, TW2865_GET_SIGNLE_REG, &twreg);
			printf("addr = 0x%x, reg = 0x%x, value = 0x%x\n", twreg.addr, twreg.reg, twreg.value);			
			break ;
		
		default :
			break ;
	}

	close(dev);

	return 0;
}
#endif
